package com.livestocksolutions.ui;

import com.livestocksolutions.R;
import com.livestocksolutions.intent.BaseActivity;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/* -- Version History --
 *  1.0 : 17 September 2012 - Initial version.
 */

/**
 * A base class to create the layout for the list of UsableItem
 * @author Adam Parr
 * @version 1.0
 * 
 * @.modified 18 October 2012
 */
public class ViewUsableItemArrayAdapter extends BaseArrayAdapter {
	/**
	 * Size of the allowable array passed
	 */
	public static final int SIZE = 10;
	/**
	 * Unique ID for each position within the String[position][{@value #ID}] array passed in the Constructor.
	 */
	public static final int ID = 0;
	/**
	 * Name position within the String[position][{@value #NAME}] array passed in the Constructor.
	 */
	public static final int NAME = 1;
	/**
	 * Description of the UsableItem position within the String[position][{@value #DESCRIPTION}]
	 * array passed in the Constructor.
	 */
	public static final int DESCRIPTION = 2;
	/**
	 * Location position within the String[position][{@value #LOCATION}] array passed in the Constructor.
	 */
	public static final int LOCATION = 3;
	/**
	 * Quantity position within the String[position][{@value #QUANTITY}] array passed in the Constructor.
	 */
	public static final int QUANTITY = 4;
	/**
	 * Quantity measurement position within the String[position][{@value #QTYMEASUREMENT}] array passed in the Constructor.
	 */
	public static final int QTYMEASUREMENT = 5;
	/**
	 * Purchase date position within the String[position][{@value #PURCHASEDATE}] array passed in the Constructor.
	 */
	public static final int PURCHASEDATE = 6;
	/**
	 * Origin date position within the String[position][{@value #ORIGIN}] array passed in the Constructor.
	 */
	public static final int ORIGIN = 7;
	/**
	 * Cost of Item position within the String[position][{@value #COST}] array passed in the Constructor.
	 */
	public static final int COST = 8;
	/**
	 * Amount that has been used position within the String[position][{@value #USED}] array passed in the Constructor.
	 */
	public static final int USED = 9;
	
	/**
	 * The list of location that will be selectable.
	 */
	protected String[][] location = new String[][]{};
	/**
	 * The list of owner that will be selectable.
	 */
	protected String[][] owner = new String[][]{};
	
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param values The UsableItem to represent in the ListView.
	 */
	public ViewUsableItemArrayAdapter(BaseActivity context, String[][] values) {
		super(context, values);
	}
	
	/**
	 * Constructor
	 * @param context The current context.
	 * @param resourceId The View to create
	 * @param values The Contacts to represent in the ListView.
	 */
	protected ViewUsableItemArrayAdapter(BaseActivity context, int resourceId, String[][] values){
		super(context, resourceId, values);
	}
 
	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		final View rowView = super.getView(position, convertView, parent);
		
		TextView itemName = (TextView) rowView.findViewById(R.id.locationName);
		TextView itemDescription = (TextView) rowView.findViewById(R.id.locationDescription);
		TextView itemLocation = (TextView) rowView.findViewById(R.id.usableItemLocationList);
		TextView itemQuantity = (TextView) rowView.findViewById(R.id.usableItemQuantity);
		TextView itemCost = (TextView)rowView.findViewById(R.id.usableItemCostPrice);
		TextView itemMeasurement = (TextView) rowView.findViewById(R.id.quantityMeasurement);
		TextView itemPurchaseDate = (TextView) rowView.findViewById(R.id.usableItemPurchaseDate);
		TextView itemOrigin = (TextView) rowView.findViewById(R.id.usableItemOriginList);
		TextView itemUsed = (TextView) rowView.findViewById(R.id.usableItemQuantityUsed);
		
		itemName.setText(values[position][NAME]);
		itemDescription.setText(values[position][DESCRIPTION]);
		itemQuantity.setText(values[position][QUANTITY]);
		itemMeasurement.setText(values[position][QTYMEASUREMENT]);
		itemCost.setText(values[position][COST]);
		String s = context.format(context.parse(values[position][PURCHASEDATE]));
		itemPurchaseDate.setText(!s.equals("")?s:context.getResources().getString(R.string.message_noText,context.getResources().getString(R.string.message_notSet)));
		itemUsed.setText(values[position][USED]);
		
		for(int x=0; x < location.length; x++){
			if(values[position][LOCATION]==null || location[x][ID].equals(values[position][LOCATION])){
				itemLocation.setText(location[x][NAME]);
				break;
			}
		}
		
		for(int x=0; x < owner.length; x++){
			if(values[position][ORIGIN]==null || owner[x][ID].equals(values[position][ORIGIN])){
				itemOrigin.setText(owner[x][NAME]);
				break;
			}
		}
		
		itemName.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				values[position][NAME]=s.toString();
			}
		});
		
		itemDescription.addTextChangedListener(new TextWatcher() {
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			public void onTextChanged(CharSequence s, int start, int before, int count) {}

			public void afterTextChanged(Editable s) {
				values[position][DESCRIPTION]=s.toString();
			}
		});
		return rowView;
	}
	
	/**
	 * Set the list of Locations that this Item could be located at.<br>
	 * The structure of this list is expected to be the First Dimension containing each Location,
	 * the Second Dimension is structured with {@link #ID} and {@link #NAME}, with ID containing
	 * a String representation of the long id of that Location, and NAME containing the name of the
	 * Location.
	 * @param locationList The String Array to use as the list of locations.
	 */
	public void setLocationList(String[][] locationList){
		location = locationList;
	}
	
	/**
	 * Set the list of Owners that this Item could be manufactured by.<br>
	 * The structure of this list is expected to be the First Dimension containing each Owner,
	 * the Second Dimension is structured with {@link #ID} and {@link #NAME}, with ID containing
	 * a String representation of the long id of that owner, and NAME containing the name of the
	 * owner.
	 * @param ownerList The String Array to use as the list of owners.
	 */
	public void setOwnerList(String[][] ownerList){
		owner = ownerList;
	}
}

